From https://plotly.com/r. Load with
library(plotly).
Main function is plot_ly, used to generate plotly figure
objects
fig <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
fig
Note that fig is a ploty object, which is a sub-class of
htmlwidget.
class(fig)
## [1] "plotly" "htmlwidget"
Plotly represents figures as nested lists of attributes, which enables it to be represented as JSON and passed to plotly.js library. To make this clearer, build new figure with several attributes:
fig <- plot_ly() %>%
add_lines(x = c("a","b","c"), y = c(1,3,2))%>%
layout(title="sample figure", xaxis = list(title = 'x'), yaxis = list(title = 'y'), plot_bgcolor = "#c7daec")
In this example, the layout attribute has sub-attributes of title, xaxis, etc. And xaxis attribute has sub-attribute of title, and this often continues through more nested named lists.
All plotly figures have three top-level attributes:
F